﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RMS.Utils.RulesManager
{
   public  class RulesManager : IRulesManager
   {
       public RulesManager()
       {
           _rules = new Dictionary<Type, Dictionary<EntityState, Delegate>>();
       }
       private Dictionary<Type, Dictionary<EntityState, Delegate>> _rules;
       private void Register(Type type, EntityState state, Delegate expression)
       {
           if (_rules.ContainsKey(type))
               if (_rules[type].ContainsKey(state))
                   _rules[type][state] = Delegate.Combine(_rules[type][state], expression);
               else
                   _rules[type].Add(state, expression);
           else
               _rules.Add(type, new Dictionary<EntityState, Delegate> { { state, expression } });
       }
       public void Register<T>(EntityState state, Action<T> expression)
       {
           Register(typeof(T), state, expression);
       }
       public Delegate Find(Type type, EntityState state)
       {
           if (this._rules.ContainsKey(type))
           {
               if (this._rules[type].ContainsKey(state))
                   return this._rules[type][state];
           }

           return null;
       }
    }
}
